﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Einstein.Data;
using Einstein.Utility;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Config
{
    public partial class Config_Operator_New : Office2007Form
    {
        public Config_Operator_New()
        {
            InitializeComponent();
            BindRole();

            txtExpireDate.Value = DateTime.Now.AddYears(1);
        }

        void BindRole()
        {
            IList<RoleInfo> list = new RoleInfoDAL().SelectAll();
            ddlRoleID.DataSource = list;
            ddlRoleID.DisplayMember = "RoleName";
            ddlRoleID.ValueMember = "RoleID";


            IList<DeptInfo> list2 = new DeptInfoDAL().SelectAll();
            list2.Insert(0,new DeptInfo { DeptName = "" });
            ddlDept.DataSource = list2;
            ddlDept.DisplayMember = "DeptName";
            ddlDept.ValueMember = "DeptID";

        }

        public Operator BindInfo
        {
            get;
            set;
        }

        public void BindData()
        {
            if (BindInfo != null)
            {
                txtUserName.Enabled = false;
                txtUserName.Text = BindInfo.UserName;
                txtChineseName.Text = BindInfo.ChineseName;
                txtPassword.Text = BindInfo.Password;
                txtExpireDate.Value = BindInfo.ExpireDate;
                ddlRoleID.SelectedValue = BindInfo.RoleId;
                ddlDept.SelectedValue = BindInfo.DeptId;
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (superValidator1.Validate())
            {
                OperatorDAL dal = new OperatorDAL();
                Operator info = new Operator { UserName = txtUserName.Text, ChineseName = txtChineseName.Text, CreateBy = Context.CurrentUserName, CreateDate = DateTime.Now, RoleId = int.Parse(ddlRoleID.SelectedValue.ToString()), Password = txtPassword.Text, ModifyDate = DateTime.Now, ModifyBy = Context.CurrentUserName, IsSys = false, ExpireDate = txtExpireDate.Value };
                info.DeptId = null;
                if (ddlDept.SelectedValue != null)
                {
                    info.DeptId = int.Parse(ddlDept.SelectedValue.ToString());
                }
                bool isEdit = false;
                if (this.BindInfo != null)
                {
                    info.UserID = this.BindInfo.UserID;
                    isEdit = true;
                }
                if (dal.Exist(info))
                {
                    BoxManager.ShowWarning("已经存在相同的帐号，请重试。");
                    txtUserName.Focus();
                    return;
                }
                bool result = isEdit ? dal.Update(info) : dal.Add(info);
                if (result)
                {
                    BoxManager.ShowSuccess();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    BoxManager.ShowFail();
                }
            }
        }
    }
}
